package com.qidi.ssh.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.qidi.ssh.bean.BaseDict;
import com.qidi.ssh.service.BaseDictService;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author zhouXinBin
 * @create 2020-10-17-13:32
 */
public class BaseDictAction extends ActionSupport implements ModelDriven<BaseDict> {
    private BaseDict baseDict=new BaseDict();
    private BaseDictService baseDictService;

    public void setBaseDictService(BaseDictService baseDictService) {
        this.baseDictService = baseDictService;
    }

    @Override
    public BaseDict getModel() {
        return baseDict;
    }

    public  String findTypeCode() throws IOException {
        List<BaseDict> baseDicts = baseDictService.findByTypeCode(this.baseDict.getDict_type_code());

        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(new String[]{"dict_sort","dict_enable","dict_memo"});
        JSONArray jsonArray = JSONArray.fromObject(baseDicts, jsonConfig);
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        ServletActionContext.getResponse().getWriter().write(jsonArray.toString());
        return NONE;

    }
}
